package likou;

import java.util.Arrays;

/**
 * @author: Tangxz
 * @email:1171702529@qq.com
 * @cate: 2020/11/06 17:25
 */
public class _1356 {
    public static void main(String[] args) {
        System.out.println(sortByBits(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8}));
    }

    public static int[] sortByBits(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = Integer.bitCount(arr[i]) * 100000 + arr[i];
        }
        Arrays.sort(arr);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i] & 100000;
        }
        Arrays.sort(arr);
        return arr;
    }

    public int[] sortByBits2(int[] arr) {
        int[] map = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            map[i] = Integer.bitCount(arr[i]) * 10000000 + arr[i];
        }
        Arrays.sort(map);
        for (int i = 0; i < map.length; i++) {
            map[i] = map[i] % 10000000;
        }
        return map;
    }
}
